package ma.platform.server.student.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import ma.plaform.server.common.log.annotation.SysLog;
import ma.platform.server.core.exception.CheckedException;
import ma.platform.server.core.util.R;
import ma.platform.server.student.entity.TbClasses;
import ma.platform.server.student.entity.TbClasses;
import ma.platform.server.student.entity.TbGrade;
import ma.platform.server.student.entity.TbStudent;
import ma.platform.server.student.service.TbClassesService;
import ma.platform.server.student.service.TbClassesService;
import ma.platform.server.student.service.TbStudentService;
import org.apache.commons.collections.CollectionUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/classes")
@Tag(description = "classes", name = "班级管理模块")
public class ClassesController {

    @Resource
    private TbClassesService tbClassesService;

    @Resource
    private TbStudentService studentService;

    @GetMapping("/page")
    public R page(@ParameterObject Page page, @ParameterObject TbClasses classes) {
        return R.ok(tbClassesService.page(page, Wrappers.<TbClasses>lambdaQuery()
                .like(StrUtil.isNotBlank(classes.getClassesName()), TbClasses::getClassesName, classes.getClassesName())
                .eq(Objects.nonNull(classes.getGradeId()), TbClasses::getGradeId, classes.getGradeId())
                .eq(Objects.nonNull(classes.getSchoolId()), TbClasses::getSchoolId, classes.getSchoolId()).orderByAsc(TbClasses::getSortd)));
    }

    @GetMapping("/list")
    public R list(@ParameterObject TbClasses classes) {
        return R.ok(tbClassesService.list(Wrappers.<TbClasses>lambdaQuery()
                .like(StrUtil.isNotBlank(classes.getClassesName()), TbClasses::getClassesName, classes.getClassesName())
                .eq(Objects.nonNull(classes.getGradeId()), TbClasses::getGradeId, classes.getGradeId())
                .eq(Objects.nonNull(classes.getSchoolId()), TbClasses::getSchoolId, classes.getSchoolId()).orderByAsc(TbClasses::getSortd)));
    }

    /**
     * 添加学生
     *
     * @return success、false
     */
    @SysLog("添加班级")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tb_classes_add')")
    public R save(@Valid @RequestBody TbClasses classes) {
        tbClassesService.save(classes);
        return R.ok(classes);
    }

    /**
     * 删除学生
     *
     * @param ids ID
     * @return R
     */
    @SysLog("删除班级")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('tb_classes_del')")
    public R removeById(@RequestBody Long[] ids) {
        List<TbStudent> classesList = studentService.list(Wrappers.<TbStudent>lambdaQuery().in(TbStudent::getClassesId, ids));
        if (CollectionUtils.isNotEmpty(classesList)){
            throw new CheckedException("该班级下有学生，无法删除");
        }
        return R.ok(tbClassesService.removeByIds(Lists.newArrayList(ids)));
    }

    /**
     * 修改学生
     *
     * @return success/false
     */
    @PutMapping
    @SysLog("修改班级")
    @PreAuthorize("@pms.hasPermission('tb_classes_edit')")
    public R updateById(@Valid @RequestBody TbClasses classes) {
        tbClassesService.updateById(classes);
        return R.ok();
    }

    /**
     * 通过ID查询信息
     * @param id ID
     */
    @GetMapping("/details/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(tbClassesService.getById(id));
    }
}
